package com.carlinfo.hospital.dao.impl;

import com.carlinfo.hospital.dao.Medicine_TypeDao;
import com.carlinfo.hospital.pojo.Doctor;
import com.carlinfo.hospital.pojo.Medicine_type;
import com.carlinfo.hospital.util.DataSourceUtil;
import com.carlinfo.hospital.util.PageInfoUtil;
import com.carlinfo.hospital.util.ReflectUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class Medicine_TypeDaoImpl implements Medicine_TypeDao {
    QueryRunner queryRunner = new QueryRunner(DataSourceUtil.getInstance());
    private String sql = "";
    @Override
    public int saveOneDao(Medicine_type medicine_type) {

        sql = "insert into medicine_type values(null,?,?,?,?,?)";
        try
        {
            Object[] objects = ReflectUtil.zh(medicine_type);
            int i = queryRunner.execute(sql, objects);
            return i;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return 0;



    }

    @Override
    public List<Medicine_type> selectListDao(PageInfoUtil pageInfoUtil, Map<String, Object> condMap) {
        sql="select * from medicine_type where 1=1 ";
        List<Object> list = new ArrayList<Object>();
        if(condMap.get("keyword")!=null && !"".equalsIgnoreCase(condMap.get("keyword")+""))
        {
            sql+=" and name like ? ";
            list.add("%"+condMap.get("keyword")+"%");
        }
        if(condMap.get("status")!=null && !"".equalsIgnoreCase(condMap.get("status")+""))
        {
            sql += " and status like ? ";
            list.add("%"+condMap.get("status")+"%");
        }
        if(condMap.get("stDate")!=null && !"".equalsIgnoreCase(condMap.get("edDate")+""))
        {
            sql+=" and (publicTime>? and publicTime<?)";
            list.add(condMap.get("stDate"));
            list.add(condMap);
        }
        if(pageInfoUtil!=null)
        {
            sql += " limit "+pageInfoUtil.getCurrentRecord()+","+pageInfoUtil.getPageSize();
        }
        System.out.println("sql:"+sql);
        String[] s = new String[list.size()];
        String[] strings = list.toArray(s);
        try
        {
            List<Medicine_type> res = queryRunner.query(sql, new BeanListHandler<Medicine_type>(Medicine_type.class), strings);
            return res;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;


    }

    @Override
    public Medicine_type selectOneDao(Map<String, Object> condMap) {

        List<Object> list = new ArrayList<Object>();
        if(!"".equalsIgnoreCase(condMap.get("name")+"")&&condMap.get("name")!=null)
        {
            sql="select * from medicine_type where name= ? ";
            list.add(condMap.get("name"));
        }
        if(!"".equalsIgnoreCase(condMap.get("id")+"")&&condMap.get("id")!=null)
        {
            sql="select * from doctor where id= ? ";
            list.add(condMap.get("id"));
        }
        try
        {
            Medicine_type res = queryRunner.query(sql, new BeanHandler<Medicine_type>(Medicine_type.class),list.get(0));
            return res;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }

        return null;

    }

    @Override
    public int updateOneDao(Medicine_type medicine_type) {
        sql = "update medicine_type set name=?,status=?,createTime=?,updateTime=?,publicTime=? where id =?";
        try
        {
            int execute = queryRunner.execute(sql, ReflectUtil.zh(medicine_type));
            return execute;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return 0;


    }

    @Override
    public int deleteOneDao(Map<String, Object> condMap) {
        List<Object> list = new ArrayList<Object>();
        if(!"".equalsIgnoreCase(condMap.get("name")+"")&&condMap.get("name")!=null)
        {
            sql="delete * from medicine_type where name= ? ";
            list.add(condMap.get("name"));
        }
        if(!"".equalsIgnoreCase(condMap.get("id")+"")&&condMap.get("id")!=null)
        {
            sql="delete * from medicine_type where id= ? ";
            list.add(condMap.get("id"));
        }
        String[] strings = new String[list.size()];
        try
        {
            int execute = queryRunner.execute(sql, list.toArray(strings));
            return execute;
        } catch (SQLException e)
        {
            e.printStackTrace();
        }
        return 0;
    }
}
